﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace TotalSystems.Core.Infrastructure
{
    public static class Extentions
    {
        public static object GetPropertyValue<T>(this T entity, string propertyName)
        {
            return entity.GetType().GetProperty(propertyName).GetValue(entity, null);
        }

        public static decimal GetDecimalValue<T>(this T entity, string propertyName)
        {
            var obj =entity.GetType().GetProperty(propertyName).GetValue(entity, null);
            return System.Convert.ToDecimal(obj);
        }

        public static void Copy(this object targetObject, object sourceObject)
        {
            //grab the type and create a new instance of that type 

            Type opSourceType = sourceObject.GetType();

            //grab the properties 
            PropertyInfo[] opPropertyInfo = opSourceType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            //iterate over the properties and if it has a 'set' method assign it from the source TO the target 
            foreach (PropertyInfo item in opPropertyInfo)
            {
                var targetPI = targetObject.GetType().GetProperty(item.Name, item.PropertyType);
                if (item.CanWrite && targetPI != null)
                {
                    //value types can simply be 'set' 
                    if (item.PropertyType.IsValueType || item.PropertyType.IsEnum || item.PropertyType.Equals(typeof(System.String)))
                    {
                        targetPI.SetValue(targetObject, item.GetValue(sourceObject, null), null);
                    }
                    //object/complex types need to recursively call this method until the end of the tree is reached 
                    else if (item.GetSetMethod() != null)
                    {
                        object opPropertyValue = item.GetValue(sourceObject, null);
                        if (opPropertyValue == null)
                        {
                            targetPI.SetValue(targetObject, null, null);
                        }
                        else
                        {
                            targetPI.SetValue(targetObject, Clone(opPropertyValue), null);
                        }
                    }
                }
            }
            //return the new item 
            //return opTarget;
        }

        public static object Clone(this object opSource)
        {
            //grab the type and create a new instance of that type 
            Type opSourceType = opSource.GetType();
            object opTarget = Activator.CreateInstance(opSourceType);

            opTarget.Copy(opSource);
            //return the new item 
            return opTarget;
        } 
    }
}
